package com.blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blog.model.Message;
import com.blog.model.UserPoJo;
import com.blog.service.IMessageService;
import com.blog.service.IUserService;
import com.blog.service.impl.MessageService;
import com.blog.service.impl.SendMailService;
import com.blog.service.impl.UserHolder;
import com.blog.service.impl.UserService;

@Controller
public class MessageController {

	@Autowired 
	IMessageService messageService;
	@Autowired
	UserHolder userHolder;
	@Autowired
	IUserService userService;
	@Autowired
	SendMailService sendMailService;
	
	@RequestMapping("/message")
	public String messagePage(Model model){
		model.addAttribute("username",userHolder.getUserName());
		UserPoJo user=userService.getUserByName(userHolder.getUserName());
		//取出当前用户的信息 匿名用户id=0
		List<Message> lm=null;
		System.out.println(userHolder.getUserName());
		if(userHolder.getUserName()=="anonymous"){
			//lm=messageService.getTitle(0);
			
		}else{
			System.out.println(user.getId());
			//没有消息会返回空
			 
		}
		System.out.println(lm);
		model.addAttribute("messages",lm);
		return "notify";
	}
	
	@RequestMapping("/sendMessage")
	@ResponseBody
	public JSONObject send(String from,String to,String content,Model model,String toAddress){
		//messageService.save(Integer.valueOf(from),Integer.valueOf(to),content);
		try {
			if(toAddress!=null&&toAddress!=""){
				sendMailService.setHOST("stmp.189.cn");
				sendMailService.setPORT("25");
				sendMailService.setUserName("17366005010");
				SendMailService.setPassWord("xpc547896321");
				sendMailService.sendTextEmail("17366005010@189.cn",toAddress,"你好","你收到了一条来自用户ID"+from+"的新消息请注意查收");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject json=new JSONObject();
		json.put("code","0");
		json.put("message","ok");
		return json;
	}
	
	
	//投稿
	@RequestMapping("/message/posttosite")
	public void postToSite(Model model,String url){
		try {
			sendMailService.setHOST("stmp.189.cn");
			sendMailService.setPORT("25");
			sendMailService.setUserName("17366005010");
			SendMailService.setPassWord("xpc547896321");
			sendMailService.sendTextEmail("17366005010@189.cn","xpcxpc.ok@163.com","有人投稿","稿件地址"+url);   
		}catch (Exception e) {}
		
		 
	}
	
	
}
